$(document).ready(function () {
     //$('form').parsley();
    getAllPermissions();
    //dynamicBinding();

});

//获得所有权限并调用showTable();
    function getAllPermissions() {
       /* let deleteStr = "";
        let updateStr = "";*/
        let columns = [
            {title: "序号", data: null,"render": function (data,type,row,meta) {
                    return data=Number(meta.row)+Number(1);
                }},
            {title: "权限名称", data: "name"},
            {title: "所属父类", data: "parentId"},
            {title: "操作代码", data: "percode"},
            {title: "restFull接口", data: "href"}/*,
            {title: "操作", data: "id","orderable": false, // 禁用排序
                "defaultContent": "","render": function (data,type,row,meta) {
                    if (hasDeletePermission) {
                        deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                    }
                    if (hasUpdatePermission) {
                        updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updatePermissionModal'>更新</button>";
                    }
                    return data = deleteStr+updateStr;}}*/
        ];
        let url ='permission/getAllPermissions';
        dataTableInit(url,columns);
    }

/*
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    /!*deleteBind('/permission/deletePermission');
    //为所有的update按钮绑定update事件
    updateBind(  '#toUpdatePermission', '/permission/updatePermission', false, false, "#updatePermissionModal");
    //为所有的add按钮绑定add事件
    addBind('#toAddPermission', '/permission/addPermission', false, false, "#addPermissionModal");
    //为add模态框绑定消失事件-清空模态框中内容*!/

    /!*$('#addPermissionModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddPermission").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updatePermissionModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdatePermission").reset();
    });*!/
        
    //为所有的delete按钮添加delete事件
    deleteBind("#permission-table-body",".data-delete","data-id",'/permission/deletePermission');
    //为所有的update按钮添加update事件
    $("#permission-table-body").on('click', ".data-update", function() {
        let _id = $(this).attr('data-id');
        console.log("updateButton");
        $("#toUpdatePermission").ajaxForm(function () {
            let id = _id;
            let parentId = $(" #updateParentId ").val();
            let name = $(" #updateName ").val();
            let percode = $(" #updatePercode ").val();
            let href = $(" #updateHref ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/permission/updatePermission',
                data: {id: id, parentId: parentId, name: name, percode: percode, href: href},
                success: function (data) {
                    if (data.code==200){
                        window.location.reload();
                    }else {
                        console.log(data);
                    }
                },
                error: function (data) {
                    console.log(data);
                }
            })
        });
    });

    //为add按钮添加add事件
    $("#toAddPermission").on('click', function() {
        $("#toAddPermission").ajaxForm(function () {
            let parentId = $(" #addParentId ").val();
            let name = $(" #addName ").val();
            let percode = $(" #addPercode ").val();
            let enable = $(" #addEnable ").val();
            let href = $(" #addHref ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/permission/addPermission',
                data: { parentId: parentId,percode:percode,enable:enable,href:href,name:name },
                success: function (data) {
                    if (data.code==200){
                        console.log(data);
                        window.location.reload();
                    }else {
                        console.log(data);
                        window.location.reload();
                    }
                    },
                error: function (data) {
                    console.log(data.msg);
                }
            })
        });
    });
}*/
